// order.js
var api = require('../../api.js');
var app = getApp();
var is_no_more = false;
var is_loading = false;
var pageSize = 5;
var pageNum = 1;
var utils = require('../../utils.js');
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgServer: api.default.img_server,

    status: -1,
    order_list: [],
    show_no_data_tip: false,
    hide: 1,
    qrcode: "",
    key:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var page = this;
    is_no_more = false;
    is_loading = false;
    page.data.key = ""
    pageNum = 1;
    page.loadOrderList(options.status || -1);
    var pages = getCurrentPages();
    if (pages.length < 9) {
      page.setData({
        show_index: true,
      });
    }
  },
     /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
      // this.loadOrderList(this.data.status);

    },
  formSubmit: function (e) {
    var page = this;
    pageNum = 1;
    page.data.key = e.detail.value.search;
    page.loadOrderList(page.data.status);
  
  }
,
  loadOrderList: function(status) {
    var access_token = wx.getStorageSync("access_token");
    if (status == undefined)
      status = -1;
    var page = this;
    page.setData({
      status: status,
    });
    wx.showLoading({
      title: "正在加载",
      mask: true,
    });
    app.request({
      url: api.order.list,
      data: {
        userid: access_token,
        pageNum: pageNum,
        pageSize: pageSize,
        key:page.data.key,
        status: page.data.status,
      },
      success: function(res) {
        console.log(res)
        if (res.code == 200) {
          page.setData({
            order_list: res.data.list,
          });
          if (res.data.total > pageSize) {
            is_no_more = false
            pageNum++
          } else {
            is_no_more = true
          }
        }
        page.setData({
          show_no_data_tip: (page.data.order_list.length == 0),
        });
      },
      complete: function() {
        wx.hideLoading();
      }
    });
  },

  statusChange: function(e) {
    var page = this;
    pageNum = 1;
    is_no_more = false;
    is_loading = false;
    var status = e.currentTarget.dataset.status
    page.setData({
      status: status
    });
    page.loadOrderList(status)
  },

  onReachBottom: function() {
    var access_token = wx.getStorageSync("access_token");
    var page = this;
    if (is_loading || is_no_more)
      return;
    is_loading = true;
    app.request({
      url: api.order.list,
      data: {
        userid: access_token,
        pageNum: pageNum,
        pageSize: pageSize,
        key:page.data.key,
        status: page.data.status,
      },
      success: function(res) {
        console.log(res)
        if (res.code == 200) {

          var order_list = page.data.order_list.concat(res.data.list);
          page.setData({
            order_list: order_list,
          });
          if (res.data.total > order_list.length) {
            is_no_more = false
            pageNum++
          } else {
            is_no_more = true
          }
        }
      },
      complete: function() {
        is_loading = false;
      }
    });
  },

  orderPay: function(e) {
    wx.showLoading({
      title: "正在提交",
      mask: true,
    });
    app.request({
      url: api.order.pay_data,
      data: {
        order_id: e.currentTarget.dataset.id,
        pay_type: "WECHAT_PAY",
      },
      complete: function() {
        wx.hideLoading();
      },
      success: function(res) {
        console.log(res);
        if (res.code == 200) {

          wx.requestPayment({
            timeStamp: res.data.timeStamp,
            nonceStr: res.data.nonceStr,
            package: res.data.package,
            signType: res.data.signType,
            paySign: res.data.paySign,
            success: function(e) {
              console.log("success");
              console.log(e);
              wx.requestSubscribeMessage({
                tmplIds: ['bPgXd4I-7E-GYafPJSNJEAQMuptkU_SYG1OscN7MM0o'],
                success(res) {
                  console.log(res)
                },
                fail(error) {
                  console.log(error)
                }
              });
            },
            fail: function(e) {
              console.log("fail");
              console.log(e);
            },
            complete: function(e) {
              console.log("complete");
              console.log(e);

              if (e.errMsg == "requestPayment:fail" || e.errMsg == "requestPayment:fail cancel") { //支付失败转到待支付订单列表
                wx.showModal({
                  title: "提示",
                  content: "订单尚未支付",
                  showCancel: false,
                  confirmText: "确认",
                  success: function(res) {
                    if (res.confirm) {
                      wx.redirectTo({
                        url: "/pages/order/order?status=9",
                      });
                    }
                  }
                });
                return;
              }


              wx.redirectTo({
                url: "/pages/order/order?status=9",
              });


            },
          });
        }
        if (res.code == 1) {
          wx.showToast({
            title: res.msg,
            image: "/images/icon-warning.png",
          });
        }

      }
    });
  },
  toPay: function(e) {
    //获取支付数据
    app.request({
      url: api.order.pay_data,
      data: {
        id: e.currentTarget.dataset.id,
        payType: 2,
      },
      success: function(res) {
        console.log(res);
        //余额支付成功
        if (res.code == 201) {
          wx.redirectTo({
            url: "/pages/order/order?status=1",
          });
          // wx.showModal({
          //   title: "提示",
          //   content: "余额支付成功",
          //   showCancel: false,
          //   confirmText: "确认",
          //   success: function(res) {
          //     if (res.confirm) {
          //       wx.redirectTo({
          //         url: "/pages/order/order?status=1",
          //       });
          //     }
          //   }
          // });
        }
        //余额支付失败
        if (res.code == 202) {
          wx.showModal({
            title: "提示",
            content: "余额支付失败，请充值后购买",
            showCancel: false,
            confirmText: "确认",
            success: function(res) {
              if (res.confirm) {
                wx.redirectTo({
                  url: "/pages/order/order?status=0",
                });
              }
            }
          });
        }
        if (res.code == 200) {
          //发起支付
          wx.requestPayment({
            timeStamp: res.data.timeStamp,
            nonceStr: res.data.nonceStr,
            package: res.data.package,
            signType: res.data.signType,
            paySign: res.data.paySign,
            success: function(e) {
              console.log('1');
              console.log(e);
              wx.requestSubscribeMessage({
                tmplIds: ['bPgXd4I-7E-GYafPJSNJEAQMuptkU_SYG1OscN7MM0o'],
                success(res) {
                  console.log(res)
                },
                fail(error) {
                  console.log(error)
                }
              });
              wx.redirectTo({
                url: "/pages/order/order?status=1",
              });
            },
            fail: function(e) {
              console.log('2');
              console.log(e);
            },
            complete: function(e) {
              console.log('3');
              console.log(e);
              if (e.errMsg == "requestPayment:fail" || e.errMsg == "requestPayment:fail cancel") { //支付失败转到待支付订单列表
                wx.showModal({
                  title: "提示",
                  content: "订单尚未支付",
                  showCancel: false,
                  confirmText: "确认",
                  success: function(res) {
                    if (res.confirm) {
                      wx.redirectTo({
                        url: "/pages/order/order?status=0",
                      });
                    }
                  }
                });
                return;
              }
              if (e.errMsg == "requestPayment:ok") {
                return;
              }
            },
          });
          return;
        }
        if (res.code == 1) {
          wx.showToast({
            title: res.msg,
            image: "/images/icon-warning.png",
          });
          return;
        }
      }
    });
  },

  orderRevoke: function(e) {
    var page = this;
    wx.showModal({
      title: "提示",
      content: "是否取消该订单？",
      cancelText: "否",
      confirmText: "是",
      success: function(res) {
        if (res.cancel)
          return true;
        if (res.confirm) {
          wx.showLoading({
            title: "操作中",
          });
          app.request({
            url: api.order.revoke,
            data: {
              orderId: e.currentTarget.dataset.id,
            },
            success: function(res) {
              wx.hideLoading();
              page.loadOrderList(page.data.status);
            }
          });
        }
      }
    });
  },

  orderConfirm: function (e) {
    var page = this;
    wx.requestSubscribeMessage({
      tmplIds: ['LYqmtO9TqZNW4MUcdIjfZ6id2t_LheP98foMtJA92j4','CKLCPm5agmZHhhXItoo7foFN_S_5bt2CWiw-_-OvIVw'],
      success(res) {
          page.orderConfirm2(e);
        console.log(res)
      }
    })
},

  orderConfirm2: function(e) {
    var page = this;
    wx.showModal({
      title: "提示",
      content: "是否确认已收到货？",
      cancelText: "否",
      confirmText: "是",
      success: function(res) {
        if (res.cancel)
          return true;
        if (res.confirm) {
          wx.showLoading({
            title: "操作中",
          });
          app.request({
            url: api.order.confirm,
            data: {
              orderId: e.currentTarget.dataset.id,
            },
            success: function(res) {
              wx.hideLoading();
              wx.showToast({
                title: res.msg,
              });
              if (res.code == 200) {
                page.loadOrderList(3);
              }
            }
          });
        }
      }
    });
  },
  orderQrcode: function(e) {
    var page = this;
    var order_list = page.data.order_list;
    var index = e.target.dataset.index;
    wx.showLoading({
      title: "正在加载",
      mask: true,
    });
    if (page.data.order_list[index].offline_qrcode) {

      page.setData({
        hide: 0,
        qrcode: page.data.order_list[index].offline_qrcode
      });
      wx.hideLoading();
    } else {
      app.request({
        url: api.order.get_qrcode,
        data: {
          order_no: order_list[index].order_no
        },
        success: function(res) {
          page.setData({
            hide: 0,
            qrcode: res.data.url
          });
        },
        complete: function() {
          wx.hideLoading();
        }
      });
    }
  },
  hide: function(e) {
    this.setData({
      hide: 1
    });
  }

});